package com.base.api.controller.userinfo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.api.service.selfOrder.SelfOrderApiService;
import com.base.log.WebLog;
import com.base.po.param.NetState;
import com.base.po.param.UserData;
import com.base.po.voip.Subscribers;
import com.base.self.common.CommMember;
import com.base.self.entity.SelfOrder;
import com.base.self.mapper.SelfOrderMapper;
import com.base.service.SubscribersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "升级等级接口")
@RestController
@RequestMapping("/levelUp")
public class LevelUpController {
    final SubscribersService subscribersService;
    final SelfOrderMapper selfOrderMapper;

    public LevelUpController(SubscribersService subscribersService, SelfOrderMapper selfOrderMapper) {
        this.subscribersService = subscribersService;
        this.selfOrderMapper = selfOrderMapper;
    }

    @WebLog(description = "团队升级")
    @ApiOperation(value = "团队升级")
    @RequestMapping(value = "/teamLevelUp", method = {RequestMethod.POST})
    public @ResponseBody
    NetState teamLevelUp(@ApiParam(required = true, name = "userId", value = "用户Id") @RequestParam String userId){
        NetState netState = new NetState();

        if (userId == null || userId.equals("")) {
            netState.setData("参数异常");
            netState.setErrorCode(NetState.PARAMS_ERROR);
            return netState;
        }

        Subscribers subscribers = null; // 用户信息
        try {
            subscribers = subscribersService.selectByPrimaryKey(userId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (subscribers == null || subscribers.getDatastatus() == false) {
            netState.setData("用户不存在或被冻结");
            netState.setErrorCode(NetState.ACCOUNT_ERROR);
            return netState;
        }

        if (subscribers.getCbonus1level() != 0){
            subscribers.setMember(subscribers.getCbonus1level());
        }else {
            UserData userData = subscribersService.selectForLevelUp(subscribers.getId());
            if (userData != null){
                if (userData.getTeamNineteen() > 0){
                    subscribers.setMember(20);
                }else if (userData.getTeamEighteen() > 0){
                    subscribers.setMember(19);
                }else if (userData.getTeamSeventeen() > 0){
                    subscribers.setMember(18);
                }else if (userData.getTeamSixteen() > 0){
                    subscribers.setMember(17);
                }else if (userData.getTeamFifteen() > 0){
                    subscribers.setMember(16);
                }else if (userData.getTeamFourteen() > 0){
                    subscribers.setMember(15);
                }else if (userData.getTeamThirteen() > 0){
                    subscribers.setMember(14);
                }else if (userData.getTeamTwelve() > 0){
                    subscribers.setMember(13);
                }else if (userData.getTeamEleven() > 0){
                    subscribers.setMember(12);
                }else if (userData.getTeamTen() > 0){
                    subscribers.setMember(11);
                }else if (userData.getTeamNine() > 0){
                    subscribers.setMember(10);
                }else if (userData.getTeamEight() > 0){
                    subscribers.setMember(9);
                }else if (userData.getTeamSeven() > 0){
                    subscribers.setMember(8);
                }else if (userData.getTeamSix() > 0){
                    subscribers.setMember(7);
                }else if (userData.getTeamFive() > 0){
                    subscribers.setMember(6);
                }else if (userData.getTeamFour() > 0){
                    subscribers.setMember(5);
                }else if (userData.getTeamThree() > 0){
                    subscribers.setMember(4);
                }else if (userData.getTeamTwo() > 0){
                    subscribers.setMember(3);
                }else if (userData.getTeamOne() > 0){
                    subscribers.setMember(2);
                }else {
                    int directHasOrderUsers = 0;
                    List<String> directUsers = subscribersService.selectForShareId(subscribers.getId());
                    for (int i = 0; i < directUsers.size(); i++) {
                        List<SelfOrder> hasOrder = selfOrderMapper.selectList(new LambdaQueryWrapper<SelfOrder>().notIn(SelfOrder::getStatus,1,5).eq(SelfOrder::getIsPartaker, 0));
                        if (hasOrder.size() > 0){
                            directHasOrderUsers++;
                        }
                    }
                    if (directHasOrderUsers >= 3){
                        subscribers.setMember(1);
                    }
                }
            }
        }

        netState.setData("用户等级更新");
        netState.setErrorCode(NetState.SUCCESS_CONN);
        return netState;
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public @ResponseBody
    NetState teamUsersTest(){
        NetState netState = new NetState();

        log.warn("[爆品购定时任务] 开始更新用户信息");

        List<Subscribers> subscribersList = subscribersService.selectAllSub(CommMember.PARENTID, null);

        for (int i = 0; i < subscribersList.size(); i++) {
            teamLevelUp(subscribersList.get(i).getId());
        }

        log.warn("[爆品购定时任务] 结束更新用户信息");

        netState.setData("成功");
        netState.setErrorCode(NetState.PARAMS_ERROR);
        return netState;
    }
}
